<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/27
 * Time: 15:58
 */
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$servername = "localhost:3306";
$username = "root";
$password = "root";
$dbname = 'test';

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM myguests";
$result = $conn->query($sql);
$output = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($output != "") {$output .= ",";}
    $output .= '{"id":"'  . $rs["id"] . '",';
    $output .= '"lastname":"'   . $rs["lastname"]        . '",';
    $output .= '"firstname":"'. $rs["firstname"]     . '"}';
}
$output ='['.$output.']';
$conn->close();
echo($output);